<?php

include_once dirname(__FILE__). '/AController.php';

class ReportController extends AController
{	
	
	// TODO 要加上权限控制
    
	/**
	 * 每日统计报告
	 */
	public function actionDiaryLog() {
		$this->layout = '/layouts/popMain';
		
		$block_title = '每日统计';
		$arr = array(
			'pagination' => array('pageSize' => 30),
			'criteria' => array(
				'contition' => '1',
				'order' => 'id DESC'
			)
		);
		if (!empty($_GET['owner_id'])) {
			$arr['criteria']['condition'] .= " AND owner_id='{$_GET['owner_id']}'";
			$block_title = $_GET['owner_id']. ' 的'. $block_title;
		}
		$dataProvider = new CActiveDataProvider('DiaryLog');
		$this->render('diaryLog', array('dataProvider' => $dataProvider, 'block_title' => $block_title));
	}
	
	public function actionSummary() {
		$this->layout = '/layouts/popMain';
		$block_title = '总览';
		$arr = array('pagination' => array('pageSize' => 21));
		
		$cond = '1';
		
		if (!empty($_GET['owner_id'])) {
			$cond .= " AND owner_id='{$_GET['owner_id']}'";
		}
		
		//每日总览
		$sql = "SELECT 
					adddate, 
					sum(joinmb) joinmb, 
					sum(vjoinmb) vjoinmb, 
					sum(totalclick) totalclick, 
					sum(validclick) validclick, 
					sum(ordernum) ordernum, 
					sum(orderamount) orderamount, 
					sum(firstordernum) firstordernum  
				FROM diary_log 
				WHERE $cond
			    GROUP BY adddate 
			    ORDER BY id DESC";
		$arr['totalItemCount'] = 20;
		$dailyProvider = new QpidSqlDataProvider($sql, $arr);
		
		//每周总览
		$sql = "SELECT 
					yearweek, 
					sum(joinmb) joinmb, 
					sum(vjoinmb) vjoinmb, 
					sum(totalclick) totalclick, 
					sum(validclick) validclick, 
					sum(ordernum) ordernum, 
					sum(orderamount) orderamount, 
					sum(firstordernum) firstordernum 
				FROM diary_log
				WHERE $cond
				GROUP BY yearweek
				ORDER BY id DESC";
		$weekProvider = new QpidSqlDataProvider($sql, $arr);
		
		//每月总览
		$sql = "SELECT 
					adddate,
					sum(joinmb) joinmb, 
					sum(vjoinmb) vjoinmb, 
					sum(totalclick) totalclick, 
					sum(validclick) validclick, 
					sum(ordernum) ordernum, 
					sum(orderamount) orderamount, 
					sum(firstordernum) firstordernum 
				FROM diary_log
				WHERE $cond
				GROUP BY (year(adddate)+month(adddate))
				ORDER BY id DESC";
		$monthProvider = new QpidSqlDataProvider($sql, $arr);
		
		$data = array(
			'block_title' => $block_title, 
			'dailyProvider' => $dailyProvider,
			'weekProvider' => $weekProvider,
			'monthProvider' => $monthProvider
		);
		$this->render('summary', $data);
	}
	
	/**
	 * 查看回报分析 
	 */
	public function actionReturn() {
		$arr = array(
			'pagination' => array('pageSize' => 20),
		);
		$dataProvider = new CActiveDataProvider('Owner', $arr);
		$this->render('return', array('block_title' => '回报分析', 'dataProvider' => $dataProvider));
	}
	
	/**
	 * 查看流量分析 
	 */
	public function actionClick() {
		$arr = array(
			'pagination' => array('pageSize' => 20),
		);
		$dataProvider = new CActiveDataProvider('Owner', $arr);
		$this->render('click', array('block_title' => '流量分析', 'dataProvider' => $dataProvider));
	}

    // Uncomment the following methods and override them if needed
    /*
    public function filters()
    {
        // return the filter configuration for this controller, e.g.:
        return array(
            'inlineFilterName',
            array(
                'class'=>'path.to.FilterClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }

    public function actions()
    {
        // return external action classes, e.g.:
        return array(
            'action1'=>'path.to.ActionClass',
            'action2'=>array(
                'class'=>'path.to.AnotherActionClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }
    */
} 